package com.ultrapower.task;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;

import com.ultrapower.commons.utils.Base6;
import com.ultrapower.model.TokenBean;

public class LoginHelper {
	
	/** 用户登录 
	 * @throws LoginException */
	
	public static Object login(TokenBean token) throws Exception {
		String message="success";
		UsernamePasswordToken pw = new UsernamePasswordToken(token.getAccountID(),"");
		pw.setRememberMe(true);//user权限使用,coockie
		Subject subject = SecurityUtils.getSubject();	
		try {
				subject.login(pw); 
        } catch (UnknownAccountException uae) {  
            message = "用户认证失败：没权限访问";             
        } catch (IncorrectCredentialsException ice) {
        	message = "用户认证失败：密码错误";  
        } catch (LockedAccountException lae) {  
        	message = "用户认证失败：用户被锁";  
        } catch (AuthenticationException e) {  
        	message = "登录失败";
            e.printStackTrace();
        }
		return message;
	}

	/**
	 * 
	* @Title: decrypToken
	* @author polly
	* @date 2018-9-17 17:12:08
	* @Description: 解密4A接口Token并转为实例
	* @param @param token
	* @return void
	* @throws Exception
	 */
	public static final TokenBean decrypToken(String bToken) throws Exception{	
		String[] tokens=Base6.decode(bToken).split("@");//解密后的令牌
		TokenBean token=new TokenBean();
		token.setSessionID(tokens[0]);
		token.setAccountID(tokens[1]);
		token.setAppID(tokens[2]);
		token.setIP(tokens[3]);
		token.setUserID(tokens[4]);
		return token;
	}
}
